#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: app_main_gui.py
Date: July 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

================================================================================
Module Description:

The tx gui service.

"""

#-------------------------------------------------------------------------------
# System imports
import sys
import os
from time import sleep
import subprocess
import Ice, IceStorm

# Application imports
import gtk_mini_tx_gui as gui
import ice_helpers
import utils

# Import the Ice interface definitions
import AcornLifeCycle
import AcornTxUpdate
import AcornTxControl
import AcornTxDsp
import AcornPersist

#===============================================================================
# Ice life-cycle events for the GUI
class LifeCycleI(AcornLifeCycle.LifeCycle):
    
    #---------------------------------------------------------------------------   
    def __init__(self, gui_impl, persist):
        self.gui_impl = gui_impl
        self.persist = persist
      
    #---------------------------------------------------------------------------
    def start(self, current=None):
        pass
        #self.gui_impl.start()
     
    #---------------------------------------------------------------------------   
    def stop(self, current=None):
        pass
        #self.gui_impl.stop()
    
    #---------------------------------------------------------------------------    
    def terminate(self, current=None):
        pass
        #self.gui_impl.terminate()
        
#===============================================================================
# Ice update events for the GUI
class TxUpdateI(AcornTxUpdate.TxUpdate):
    
    #---------------------------------------------------------------------------   
    def __init__(self, gui_impl, persist):
        self.gui_impl = gui_impl
        self.persist = persist
    
    #---------------------------------------------------------------------------   
    def freqUpdate(self, freq, current=None):
        pass
        #self.gui_impl.thd_freq_update(freq)
        
    #---------------------------------------------------------------------------  
    def oscUpdate(self, osc, current=None):
        pass
        #self.gui_impl.thd_osc_update(osc)
    
    #---------------------------------------------------------------------------  
    def modeUpdate(self, mode, current=None):
        pass
        #self.gui_impl.thd_mode_update(mode)
      
    #---------------------------------------------------------------------------  
    def filterUpdate(self, filter, current=None):
        pass
        #self.gui_impl.thd_filter_update(filter)

#===============================================================================
# Ice config events for the GUI
class TxConfigI(AcornTxUpdate.TxConfig):
    
    #---------------------------------------------------------------------------   
    def __init__(self, gui_impl, persist):
        self.gui_impl = gui_impl
        self.persist = persist
      
    #---------------------------------------------------------------------------   
    def configUpdate(self, name, current=None):
        # Update the gtk_options as there may be more than one options GUI running
        pass
        #self.gui_impl.update_option(name)
        
#===============================================================================
# The main class of 'a' GUI application
class AppTxGui(Ice.Application):
  
    #---------------------------------------------------------------------------
    def __init__(self):
        pass
            
    #---------------------------------------------------------------------------
    # Run the app
    def run(self, args):
       
        #-----------------------------------------------------------------------
        # persist interface
        persist = ice_helpers.get_interface('GUI', self.communicator, AcornPersist.PersistPrx, 'Persist.Proxy')
            
        # Paths
        glade_path = utils.persist_get(persist, 'Profile', 'path.glade')
        HOME = os.environ['ACORN_HOME']
        ice_config = utils.persist_get(persist, 'Profile', 'path.iceconfig')
        
        #-----------------------------------------------------------------------
        # Event names used here
        publish_to_lc = 'life_cycle_events'
        publish_to_tx_control = 'tx_control_events'
        publish_to_tx_dsp = 'tx_dsp_events'
        publish_to_tx_updates = 'tx_update_events'
        subscribe_to_tx_updates = 'tx_update_events'
        subscribe_to_tx_config = 'tx_config_events'
        subscribe_to_lc = 'life_cycle_events'
  
        #-----------------------------------------------------------------------
        # This is the same for both publishers and subscribers
        # Get the IceStorm topic manager interface
        manager = ice_helpers.get_topic_manager('GUI', self.communicator)
    
        # Retrieve the subscriber topic
        tx_update_topic = ice_helpers.get_topic('GUI', manager, subscribe_to_tx_updates)
        life_cycle_topic = ice_helpers.get_topic('GUI', manager, subscribe_to_lc)
        tx_config_topic = ice_helpers.get_topic('GUI', manager, subscribe_to_tx_config)
              
        # Retrieve the publisher topics
        publish_topic_life_cycle = ice_helpers.get_topic('GUI', manager, publish_to_lc)
        publish_topic_tx_control = ice_helpers.get_topic('GUI', manager, publish_to_tx_control)
        publish_topic_tx_dsp = ice_helpers.get_topic('GUI', manager, publish_to_tx_dsp)
        publish_topic_tx_update = ice_helpers.get_topic('GUI', manager, publish_to_tx_updates)
              
        #-----------------------------------------------------------------------
        # Publisher side
        # Get the topic's publisher object, and create a hw proxy with
        # the mode specified as an argument of this application.
        #
        publisher_life_cycle = publish_topic_life_cycle.getPublisher();
        publisher_life_cycle = publisher_life_cycle.ice_oneway();        
        life_cycle_publisher = AcornLifeCycle.LifeCyclePrx.uncheckedCast(publisher_life_cycle)
        
        publisher_tx_control = publish_topic_tx_control.getPublisher();
        publisher_tx_control = publisher_tx_control.ice_oneway();        
        tx_control_publisher = AcornTxControl.TxControlPrx.uncheckedCast(publisher_tx_control)
        
        publisher_tx_dsp = publish_topic_tx_dsp.getPublisher();
        publisher_tx_dsp = publisher_tx_dsp.ice_oneway();        
        tx_dsp_publisher = AcornTxDsp.TxDspPrx.uncheckedCast(publisher_tx_dsp)       
        
        publisher_tx_update = publish_topic_tx_update.getPublisher();
        publisher_tx_update = publisher_tx_update.ice_oneway();        
        tx_update_publisher = AcornTxUpdate.TxUpdatePrx.uncheckedCast(publisher_tx_update)
        
        #-----------------------------------------------------------------------
        # Initialise the GUI as its needed for the adapter and requires the publishers
        # Set up the initial config
        init = {}
        gui_impl = gui.AppGui(glade_path, persist, life_cycle_publisher, tx_control_publisher, tx_dsp_publisher, tx_update_publisher, ice_config, init)
        
        #-----------------------------------------------------------------------
        # Subscriber side
        # Create an adapter for the subscribers   
        adapter_1 = self.communicator().createObjectAdapter("TxUpdate.Subscriber")
        subId_1 = Ice.Identity()
        subId_1.name = Ice.generateUUID()        
        # Add the update interface
        subscriber_1 = adapter_1.add(TxUpdateI(gui_impl, persist), subId_1)
        # Subscribe to the topic - set up proxy
        ice_helpers.subscribe('GUI', tx_update_topic, subscriber_1, id)
        # Now we can receive callbacks   
        adapter_1.activate()
        
        adapter_2 = self.communicator().createObjectAdapter("LifeCycle.Subscriber")
        subId_2 = Ice.Identity()
        subId_2.name = Ice.generateUUID()        
        # Add the update interface
        subscriber_2 = adapter_2.add(LifeCycleI(gui_impl, persist), subId_2)
        # Subscribe to the topic - set up proxy
        ice_helpers.subscribe('GUI', life_cycle_topic, subscriber_2, id)
        # Now we can receive callbacks   
        adapter_2.activate()
        
        adapter_3 = self.communicator().createObjectAdapter("TxConfig.Subscriber")
        subId_3 = Ice.Identity()
        subId_3.name = Ice.generateUUID()        
        # Add the update interface
        subscriber_3 = adapter_3.add(TxConfigI(gui_impl, persist), subId_3)
        # Subscribe to the topic - set up proxy
        ice_helpers.subscribe('GUI', tx_config_topic, subscriber_3, id)
        # Now we can receive callbacks   
        adapter_3.activate()
                 
        #-----------------------------------------------------------------------
        # Update all listeners state by sending events into the system
        
        #-----------------------------------------------------------------------
        print 'Acorn-SDR (TX GUI) running..'
        # Start the GUI
        quit = gui_impl.main()
        # This only returns when the GUI is closed
        
        # Unsubscribe all subscribed objects.
        # causes issues at the moment
        #tx_update_topic.unsubscribe(subscriber_1)
        #tx_life_cycle_topic.unsubscribe(subscriber_2)
        #tx_config_topic.unsubscribe(subscriber_3)
        
#===============================================================================
#===============================================================================
# Start from the command line
if __name__ == "__main__":
   
  app = AppTxGui()
  app.main(sys.argv, sys.argv[1])
  
  # Announce we are going 
  print 'Acorn-SDR (TX GUI) closing..'
  
